//
//  PongAudio.m
//
//  Created by Jonathan Saggau on 2007-08-12.
//  Copyright (c) 2007 __MyCompanyName__. All rights reserved.
//

#import "PongAudio.h"
#import "AVItem.h"
#import "AVController.h"
#import "AVQueue.h"


@interface PongAudio (PrivateAPI)
-(void)play:(AVItem *)item;
@end

@implementation PongAudio


- (id)init
{
	self = [super init];
	if (nil!= self)
	{
		NSError *err;
		NSString *path = [[NSBundle mainBundle] pathForResource:@"PongBounce" ofType:@"wav" inDirectory:@"/"];
		bounce = [[AVItem alloc] initWithPath:path error:&err];
		if (nil != err)
		{
			NSLog(@"err! = %@  \n item = [[AVItem alloc] initWithPath:path error:&err];", err); 
			exit(1);
		}
		
		path = [[NSBundle mainBundle] pathForResource:@"PongLose" ofType:@"wav" inDirectory:@"/"];
		loser = [[AVItem alloc] initWithPath:path error:&err];
		if (nil != err)
		{
			NSLog(@"err! = %@  \n item = [[AVItem alloc] initWithPath:path error:&err];", err); 
			exit(1);
		}
		
		controller = [[AVController alloc] init];
		[controller setDelegate:self];
		q = [[AVQueue alloc] init];
	    [q appendItem:bounce error:&err];
		if (nil != err)
		{
			NSLog(@"err! = %@ \n [q appendItem:item error:&err];", err); 
			exit(1);
		}
		
		[q appendItem:loser error:&err];
		if (nil != err)
		{
			NSLog(@"err! = %@ \n [q appendItem:item error:&err];", err); 
			exit(1);
		}
	}
	return self;
}

- (void)dealloc
{
	[bounce release]; bounce = nil;
	[loser release]; loser = nil;
	[q release]; q = nil;
	[controller release]; controller = nil;
	[super dealloc];
}

-(void)playBounce
{
	[self stop];
	[self play:bounce];
}

-(void)playLoser
{
	[self stop];
	[self play:loser];
}

-(void)play:(AVItem *)item;
{
	[controller setCurrentItem:item];
	
	//play NOW
	[controller setCurrentTime:(double)0.0];
	//should maybe check this, too.
	//- (BOOL)isCurrentItemReady;
	NSError *err;
	[controller play:&err];
	if(nil != err)
	{
		NSLog(@"err! = %@    [controller play:&err];", err); 
		exit(1);
	}
}

-(void)stop;
{
	[controller pause];
}

- (void)queueItemWasAdded:(id)fp8
{
	//LogMethod();
}
@end
